/**
 *
 * The MIT License
 *
 * Copyright (c) 2008 Board of Trustees, Leland Stanford Jr. University. All rights reserved.
 * Copyright (c) 2009 Ben Suter. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.macdadi.ks.client.views;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.VerticalPanel;
import org.macdadi.core.client.auth.AuthorizationException;
import org.macdadi.core.client.domain.*;
import org.macdadi.ks.client.View;
import org.macdadi.ks.client.ViewContainer;
import org.macdadi.ks.client.components.ValuesChart;

/**
 * @author Ben Suter <ben.suter@gmail.com>
 */
public class Values extends View
{
    protected FlexTable table;
    protected ProjectInfo project;
    protected ValuesChart valuesChart;
    protected final String CHART_URL_BASE = "http://chart.apis.google.com/chart?";

    public static ViewInfo init()
    {
        String info = "<p>Visualize the value of each option</p>";

        return new ViewInfo("Values", info)
        {
            public View createInstance(ViewContainer container)
            {
                return new Values(container);
            }

            public String getColor()
            {
                return "#bf2a2a";
            }
        };
    }

    public Values(ViewContainer container)
    {
        this.container = container;

        VerticalPanel mainPanel = new VerticalPanel();
        mainPanel.setStyleName("macdadi-Values");
        mainPanel.setWidth("100%");

        valuesChart = new ValuesChart();
        valuesChart.setChartTitle("Value of each Option (for all Goals, weighted by Preferences)");
        mainPanel.add(valuesChart);

    //    table = new FlexTable();
    //    mainPanel.add(table);

        initWidget(mainPanel);
    }

    public void onShow()
    {
     //   populateTable();
        populateChart();
    }

    public void onHide()
    {
        valuesChart.clear();
    //    table.clear();
    }

    protected void populateTable()
    {
        project = container.getCurrentProject();

        DataService.App.getInstance().getImpacts(project.getId(), container.getCurrentAuthToken(), new AsyncCallback()
        {
            public void onFailure(Throwable caught)
            {
                GWT.log("Failed to populate the values table", caught);
                if (caught instanceof AuthorizationException) {
                   // TODO: do not use Window.alert for error feedback
                   Window.alert("Please login to view values for " + project.getName());
                }
            }

            public void onSuccess(Object result)
            {
                ImpactMatrix matrix = (ImpactMatrix) result;

                /* Create options header row */
                Option[] options = matrix.getOptions();
                for (int i = 0; i < options.length; i++) {
                    /* The header row displays the options, from the second heading on*/
                    table.setText(0, i + 1, options[i].getName());
                    table.getFlexCellFormatter().setStyleName(0, i + 1, "impact-option-header");
                }

                /* Create goals header column */
                WeightedGoal[] goals = matrix.getGoals();
                for (int i = 0; i < goals.length; i++) {
                    /* The left-most column displays the Goals, from the second row on*/
                    table.setText(i + 1, 0, goals[i].getName());
                    table.getFlexCellFormatter().setStyleName(i + 1, 0, "impact-goal-header");
                }

                /* Fill in cells with the impact values */
                WeightedGoal g;
                for (int i = 0; i < goals.length; i++) {
                    g = goals[i];
                    for ( int j = 0; j < options.length; j++) {
                        setImpactTableCell(i + 1, j + 1, matrix.getImpact(g, options[j]));
                    }
                }

            }
        });
    }


    protected void setImpactTableCell(int row, int col, Impact impact)
    {
        table.setText(row, col, Integer.toString(impact.getValue()));
    }


    protected void populateChart()
    {
        project = container.getCurrentProject();
        valuesChart.populate(project.getId(), container.getCurrentAuthToken());
    }
}